import { useState, useEffect } from "react"
import { throttle } from "underscore"
export default function useScrollPosition() {
    // 获取滚动条位置
    const [scrollX, setScrollX] = useState(0)
    const [scrollY, setScrollY] = useState(0)

    // 监听滚动条位置
    useEffect(() => {
        const handleScroll = throttle(() => {
            setScrollX(window.scrollX)
            setScrollY(window.scrollY)
        }, 100)

        window.addEventListener("scroll", handleScroll)
        return () => {
            window.removeEventListener("scroll", handleScroll)
        }
    }, [])

    // 返回滚动条位置
    return { scrollX, scrollY }
}